﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK.Graphics.OpenGL;
using OpenTK;
using System.Drawing;

namespace MyOpenGame
{
    public class Inventory : AModel
    {
        private bool _animateUp;
        private List<Defence> _items;

        public Inventory() : base()
        {
            GameUtility gameUtility = new GameUtility();
            _points = gameUtility.GetInventoryPoints();
            _items = new List<Defence>();
            _texture = Texture.getInventory();
            _moveStep = -1;
            _yTopLimit = 0;
            _yBottomLimit = -91;
            _lifePoints = 1000;
            _animateUp = false;
        }

        public override void Draw()
        {
            if (_animateUp && _movedX < 91)
            {
                _movedX += _moveStep;
            }
            else if (!_animateUp && _movedX != 0)
            {
                _movedX += _moveStep;
            }

            GL.LoadIdentity();
            GL.BindTexture(TextureTarget.Texture2D, _texture);
            GL.Begin(BeginMode.Quads);
            GL.TexCoord2(0, 0); GL.Vertex2(_points[0].X, _points[0].Y + _movedX);
            GL.TexCoord2(1, 0); GL.Vertex2(_points[1].X, _points[1].Y + _movedX);
            GL.TexCoord2(1, 1); GL.Vertex2(_points[2].X, _points[2].Y + _movedX);
            GL.TexCoord2(0, 1); GL.Vertex2(_points[3].X, _points[3].Y + _movedX);
            GL.End();
        }

        public override List<Vector2> GetCurrentPosition()
        {
            var currentPotition = new List<Vector2>();
            currentPotition.Add(new Vector2(_points[0].X, _points[0].Y + _movedX));
            currentPotition.Add(new Vector2(_points[1].X, _points[1].Y + _movedX));
            currentPotition.Add(new Vector2(_points[2].X, _points[2].Y + _movedX));
            currentPotition.Add(new Vector2(_points[3].X, _points[3].Y + _movedX));
            return currentPotition;
        }

        public void ChangeState()
        {
            if (_animateUp)
            {
                _animateUp = false;
                _moveStep = -1;
                
                _yBottomLimit = -91;
            }
            else
            {
                _animateUp = true;
                _moveStep = 1;
                _yTopLimit = 91;
            }
        }

        public DefenderState GetSelectedItem(Point point)
        {
            DefenderState defenceState = new SourceSelected();
            if (GetCurrentPosition()[0].X <= point.X && GetCurrentPosition()[0].X + 50 >= point.X
                && GetCurrentPosition()[0].Y >= point.Y && GetCurrentPosition()[0].Y - 50 <= point.Y)
            {
                defenceState = new Building();
                
            }
            return defenceState;
        }

    }
}
